package a2025_02_10;

import java.util.Stack;
//150 逆波兰表达式求值
public class evaluate_reverse_polish_notation {
    public static void main(String[] args) {
        Stack<Integer> set=new Stack<>();
        String[] tokens={"4","13","5","/","+"};
        for(int i=0;i<tokens.length;i++){
            String temp=tokens[i];
            //判断不是操作符
            if(!fund(temp)){
                set.push(func(temp));
            }else{
                int top1=set.pop();
                int top2=set.pop();
                if(temp.equals("+")) set.push(top1+top2);
                else if(temp.equals("-")) set.push(top1-top2);
                else if(temp.equals("*")) set.push(top1*top2);
                else if(temp.equals("/")) set.push(top1/top2);
            }
        }

    }
    public static int func(String str){
        int ret=0;
        for(int i=0;i<str.length();i++){
            char ch=str.charAt(i);
            if(ch>='0'&&ch<='9'){
                ret=ret*10+ch-'0';
            }
        }
        if(str.charAt(0)=='-') return -ret;
        return ret;
    }
    public static boolean fund(String temp){
        return temp.equals("+")||temp.equals("-")||temp.equals("*")||temp.equals("/");
    }

}
